package com.elong.businesstravel.modules.member;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;

import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.baseactivity.BaseWebViewActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.protocol.ProtocolDef;

/**
 * WebView界面
 * 
 * @author lenovo
 * 
 */
public class WebViewActivity extends BaseWebViewActivity {

	private String mTitleString;
	private String mUrlString;
	private String mDataString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);

		if (null == mSavedInstanceState) {
			mTitleString = getIntent().getStringExtra("title");
			mUrlString = getIntent().getStringExtra("url");
			mDataString = getIntent().getStringExtra("data");
		} else {
			mTitleString = mSavedInstanceState.getString("title");
			mUrlString = mSavedInstanceState.getString("url");
			mDataString = mSavedInstanceState.getString("data");
		}

		mWebView = (WebView) findViewById(R.id.webView);
		initWebView(mWebView);
		if (!TextUtils.isEmpty(mUrlString)) {
			mWebView.loadUrl(mUrlString);
		}
		if (!TextUtils.isEmpty(mDataString)) {
			mWebView.loadData(mDataString, "text/html; charset=UTF-8", null);
		}

		setListener();
		setTitleNavBar();
	}

	@Override
	protected void setListener() {
		mWebView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			public void onPageFinished(WebView view, String url) {

			}

			public void onPageStarted(WebView view, String url, Bitmap favicon) {

			}
		});
	}

	@Override
	protected void setTitleNavBar() {

		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(mTitleString);
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 返回
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		outState.putString("title", mTitleString);
		outState.putString("url", mUrlString);
		outState.putString("data", mDataString);

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != savedInstanceState) {
			mTitleString = savedInstanceState.getString("title");
			mUrlString = savedInstanceState.getString("url");
			mDataString = savedInstanceState.getString("data");
		}
	}

}
